package com.van.gateway.utils;

import com.van.entity.ResPonObject;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * @author VanKa qq:2395073039
 * @date 2021/8/17 10:15
 */
public class GatewayServletUtils {

    /**
     * 认证错误输出
     *
     * @param r 响应对象
     * @param o 错误信息
     * @return Mono
     */
    public static Mono<Void> authError(ServerHttpResponse r, ResPonObject o) {
//        r.setStatusCode(HttpStatus.UNAUTHORIZED);
        r.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        String returnStr = o.toJson();
        DataBuffer buffer = r.bufferFactory().wrap(returnStr.getBytes(StandardCharsets.UTF_8));
        return r.writeWith(Flux.just(buffer));
    }
}
